import { z } from 'zod'

// 插件审核相关模式
export const approvePluginSchema = z.object({
  reason: z.string().optional().describe('Approval reason (optional)')
})

export const rejectPluginSchema = z.object({
  reason: z.string()
    .min(10, 'Rejection reason must be at least 10 characters long')
    .max(500, 'Rejection reason must not exceed 500 characters')
    .describe('Detailed reason for rejection')
})

export const suspendPluginSchema = z.object({
  reason: z.string()
    .min(10, 'Suspension reason must be at least 10 characters long')
    .max(500, 'Suspension reason must not exceed 500 characters')
    .describe('Detailed reason for suspension')
})

// 用户管理相关模式
export const suspendUserSchema = z.object({
  reason: z.string()
    .min(10, 'Suspension reason must be at least 10 characters long')
    .max(500, 'Suspension reason must not exceed 500 characters')
    .describe('Detailed reason for user suspension')
})

// 查询参数模式
export const paginationSchema = z.object({
  page: z.string()
    .optional()
    .transform(val => val ? parseInt(val, 10) : 1)
    .refine(val => val > 0, 'Page must be greater than 0'),
  limit: z.string()
    .optional()
    .transform(val => val ? parseInt(val, 10) : 20)
    .refine(val => val > 0 && val <= 100, 'Limit must be between 1 and 100')
})

export const pluginFiltersSchema = z.object({
  status: z.enum(['pending', 'approved', 'rejected', 'suspended']).optional(),
  category: z.string().optional(),
  developerId: z.string().optional(),
  search: z.string().optional()
}).merge(paginationSchema)

export const userFiltersSchema = z.object({
  status: z.enum(['active', 'suspended', 'inactive']).optional(),
  role: z.enum(['developer', 'admin']).optional(),
  verified: z.string()
    .optional()
    .transform(val => val === 'true' ? true : val === 'false' ? false : undefined),
  search: z.string().optional()
}).merge(paginationSchema)

export const auditLogFiltersSchema = z.object({
  action: z.string().optional(),
  resourceType: z.enum(['plugin', 'user', 'api_key']).optional(),
  userId: z.string().optional(),
  startDate: z.string()
    .optional()
    .transform(val => val ? new Date(val) : undefined)
    .refine(val => !val || !isNaN(val.getTime()), 'Invalid start date'),
  endDate: z.string()
    .optional()
    .transform(val => val ? new Date(val) : undefined)
    .refine(val => !val || !isNaN(val.getTime()), 'Invalid end date')
}).merge(paginationSchema)

// 管理员权限验证
export const adminPermissionSchema = z.object({
  action: z.enum([
    'plugin:approve',
    'plugin:reject', 
    'plugin:suspend',
    'plugin:restore',
    'user:suspend',
    'user:restore',
    'user:view',
    'stats:view',
    'audit:view'
  ]),
  resourceId: z.string().optional()
})

// 批量操作模式
export const batchPluginActionSchema = z.object({
  pluginIds: z.array(z.string()).min(1, 'At least one plugin ID is required'),
  action: z.enum(['approve', 'reject', 'suspend']),
  reason: z.string().optional()
})

export const batchUserActionSchema = z.object({
  userIds: z.array(z.string()).min(1, 'At least one user ID is required'),
  action: z.enum(['suspend', 'restore']),
  reason: z.string().optional()
})

// 系统配置模式
export const systemConfigSchema = z.object({
  maxPluginsPerDeveloper: z.number().min(1).max(100).optional(),
  maxApiKeysPerDeveloper: z.number().min(1).max(20).optional(),
  autoApprovalEnabled: z.boolean().optional(),
  maintenanceMode: z.boolean().optional(),
  registrationEnabled: z.boolean().optional()
})

// 响应类型定义
export type ApprovePluginRequest = z.infer<typeof approvePluginSchema>
export type RejectPluginRequest = z.infer<typeof rejectPluginSchema>
export type SuspendPluginRequest = z.infer<typeof suspendPluginSchema>
export type SuspendUserRequest = z.infer<typeof suspendUserSchema>
export type PluginFilters = z.infer<typeof pluginFiltersSchema>
export type UserFilters = z.infer<typeof userFiltersSchema>
export type AuditLogFilters = z.infer<typeof auditLogFiltersSchema>
export type BatchPluginAction = z.infer<typeof batchPluginActionSchema>
export type BatchUserAction = z.infer<typeof batchUserActionSchema>
export type SystemConfig = z.infer<typeof systemConfigSchema>
